<template>
  <div class="rankList">
    <div class="thead">
      <div class="tr clearfix">
        <div :class="`td ${index === columns.length-1 ? 'fr': 'fl'}`"
          v-for="(item,index) in columns"
          :key="item.feild"
          :style="getStyleByIndex(index)"
          style="min-height: 10px;"
        >
          {{ item.title }}
      </div>
      </div>
    </div>
    <div class="tbody">
      <div class="tr clearfix" v-for="(row, i) in tableData" :key="row.id">
        <div :class="`td ${index === columns.length-1 ? 'fr': 'fl'}`" v-for="(item, index) in columns" :key="item.title" :style="getStyleByIndex(index)">
          <template v-if="columns.length === 3">
            <template v-if="index === 0">
              <div class="rank" v-if="type === 'rank'">
                <div :class="['number', 'rank'+i ]">{{ i+1 }}</div>
              </div>
              <i v-else class="colorTip" :style="{ borderTop: '6px solid ' + colors[i] }"></i>
            </template>
            <template v-else>
              <span class="ellipsis" :title="row[item.feild]" v-if="item.formatter">{{
                item.formatter(row[item.feild])
              }}</span>
              <span class="ellipsis" :title="row[item.feild]" v-else>{{ row[item.feild] }}</span>
            </template>
          </template>
          <template v-else>
            <template v-if="index === 0">
              <div class="rank" v-if="type === 'rank'">
                <div :class="['number', 'rank'+i ]">{{ i+1 }}</div>
              </div>
              <i v-else class="colorTip" :style="{ borderTop: '6px solid ' + colors[i] }"></i>
            </template>
              <span class="ellipsis" :title="row[item.feild]" v-if="item.formatter">{{
                item.formatter(row[item.feild])
              }}</span>
              <span class="ellipsis" :title="row[item.feild]" v-else>{{ row[item.feild] }}</span>
          </template>
          
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "RankList",
  props: {
    type: {
      type: String,
      default: 'rank'
    },
    columns: {
      type: Array,
      required: true,
    },
    tableData: {
      type: Array,
      required: true,
      default: [],
    },
    colors: {
      type: Array,
      default: () => [
        "#F14344",
        "#FF831F",
        "#E6CF57",
        "#0690F3",
        "#00C662",
        "#25D5E8"
      ]
    }
  },
  data() {
    return {};
  },
  methods: {
    getStyleByIndex(index) {
      if (this.columns.length === 3) {
        if(index === 0) {
          return { width: "15%" }
        } else if (index === 1) {
          return { textAlign: "left", width: "50%" }
        } else {
          return { textAlign: "right", width: "35%" }
        }
      } else if (this.columns.length === 2) {
        if(index === 0) {
          return { width: "50%" }
        } else if (index === 1) {
          return { width: "50%", textAlign: 'right' }
        }
      } else if (this.columns.length === 4) {
        if(index === 0) {
          return { width: "10%" }
        } else if (index >= 1) {
          return { textAlign: "center", width: "30%" }
        } 
      }
      
    }
  }
};
</script>

<style lang="scss" scoped>
.rankList {
  width: 100%;
  font-size: 12px;
  line-height: 28px;
  color: #fff;
  border-collapse:collapse;
  border-spacing:0px;
  .ellipsis{
    display:block;
    align-items: center;
    height: 21px;
    line-height: 21px;
  }
  .thead {
    .tr {
      padding-right: 20px;
      .td {
        color: #cfeefc !important;
        font-weight: 400 !important;
        margin-left: -6.6px;
      }
      .td:first-child {
        position: relative;
        padding-left: 18px;
        margin-left: 0;
      }
    }
  }
  .tbody {
    max-height: 140px;
    overflow-y: auto;
    .tr {
      background: rgba(0,9,26,0.3);
      margin-bottom: 4px;
      padding-right: 20px;
      // line-height: 21px;
      padding: 2px 0;
      .td {
        &:first-child {
          position: relative;
          padding-left: 18px;
          // line-height: 21px;
        }
      }
    }
  }

  .colorTip {
    position: absolute;
    top: 0;
    left: 0;
    width: 0;
    height: 0;
    border-top: 6px solid red;
    border-bottom: 6px solid transparent;
    border-left: 6px solid transparent;
    border-right: 6px solid transparent;
    transform: rotate(135deg) translate(0px,7px);
  }
  .rank {
    display: flex;
    // justify-content: center;
    align-items: center;
    margin-top: 4px;
    .number {
      width: 18px;
      height: 18px;
      /* margin-right: 10px; */
      font-size: 12px;
      font-family: Arial Narrow, Arial Narrow-Bold;
      font-weight: 700;
      text-align: center;
      color: #ff582e;
      line-height: 18px;
      letter-spacing: 0.56px;
      border: 1px solid #ff582e;
      border-radius: 50%;
      &.rank0 {
        color: #ff582e;
        border: 1px solid #ff582e;
      }
      &.rank1 {
        color: #FF6F0F;
        border: 1px solid #FF6F0F;
      }
      &.rank2 {
        color: #E4AB00;
        border: 1px solid #E4AB00;
      }
      &.rank3 {
        color: #07C8FF;
        border: 1px solid #07C8FF;
      }
      &.rank4 {
        color: #07C8FF;
        border: 1px solid#07C8FF;
      }
    }
  }
  .timeNumber {
    font-weight: bold;
  }
}
</style>